package controllers;

import dto.retjson.RetDataDto;
import models.user.AniUser;
import play.libs.Json;
import play.mvc.*;
import services.api.AniApiEditService;
import services.api.AniApiRegisterService;
import services.user.*;
import utils.*;

import java.util.Map;

/**
 * User: yeh
 * Date: 2/26/13
 * Time: 4:01 PM
 * For:  wilddragon
 */
public class AniApiController extends BaseController {
    // --register api
    public static Result registerApi(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            Map<String, String> apiRegInfoArgs = MVCUtils.getRequestArgs(request());
            AniApiRegisterService.registerApi(apiRegInfoArgs, curUser);
        }catch(Exception e){
            e.printStackTrace();
            return Controller.ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return Controller.ok(Json.toJson(new RetDataDto()));
    }

    // --edit api
    public static Result updateApi(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            Map<String, String> apiEditInfoArgs = MVCUtils.getRequestArgs(request());
            AniApiEditService.updateApiInfo(apiEditInfoArgs, curUser);
        }catch(Exception e){
            e.printStackTrace();
            return Controller.ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return Controller.ok(Json.toJson(new RetDataDto()));
    }
    // --delete api
    public static Result deleteApi(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            Map<String, String> apiDelInfoArgs = MVCUtils.getRequestArgs(request());
            AniApiEditService.deleteApi(apiDelInfoArgs, curUser);
        }catch(Exception e){
            e.printStackTrace();
            return Controller.ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return Controller.ok(Json.toJson(new RetDataDto()));
    }
}
